//
//  HistoryViewController.m
//  e-nurse
//
//  Created by  on 10/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "HistoryViewController.h"
#import "PeakValueItemViewController.h"
#import "ExceedStdItemViewController.h"
#import "RecordItemViewController.h"
#import "DateUtils.h"
#import "Database.h"
#import "ServerProxy.h"

@interface HistoryViewController ()

@property (strong, nonatomic) NSMutableArray *histories;
@property (strong, nonatomic) PeakValueItemViewController* peakValueItemVC;
@property (strong, nonatomic) ExceedStdItemViewController* exceedStdItemVC;

-(void)loadHistories;
-(void)loadHistories:(NSDate*)startDay endDay:(NSDate*)endDay;
-(void)initializeHistories;
-(void)reloadHistories;

@end

@implementation HistoryViewController
@synthesize dateRangeSegControl = _dateRangeSegControl;
@synthesize histories = _histories;
@synthesize peakValueItemVC = _peakValueItemVC;
@synthesize exceedStdItemVC = _exceedStdItemVC;
@synthesize tableView = _tableView;

-(void)dealloc
{
    self.histories = nil;
    self.peakValueItemVC = nil;
    self.exceedStdItemVC = nil;
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self loadHistories];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.dateRangeSegControl = nil;
    self.histories = nil;
    self.peakValueItemVC = nil;
    self.exceedStdItemVC = nil;
    self.tableView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.histories == nil)
        return 0;
    return [self.histories count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HistoryItemViewController* controller = (HistoryItemViewController*)[self.histories objectAtIndex:indexPath.row];
    return [controller getItemHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (UITableViewCell*)[[self.histories objectAtIndex:indexPath.row] view];
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row < 2) return;
    
    
}

-(void)dateRangeChanged:(id)sender
{
    [self loadHistories];
}

-(void)loadHistories
{
    if(self.histories == nil)
    {
        [self initializeHistories];
    }
    
    NSDate* currentDate = [[NSDate alloc] init];
    NSDate* startDate = nil;
    NSDate* endDate = nil;

    switch([self.dateRangeSegControl selectedSegmentIndex])
    {
        case 0:
            [DateUtils getWeeksRange:(&startDate) endDay:(&endDate) currentDay:currentDate weekCount:1];
            break;
        case 1:
            [DateUtils getWeeksRange:(&startDate) endDay:(&endDate) currentDay:currentDate weekCount:2];
            break;
        case 2:
            [DateUtils getMonthsRange:(&startDate) endDay:(&endDate) currentDay:currentDate monthCount:1];
            break;
        case 3:
            [DateUtils getMonthsRange:(&startDate) endDay:(&endDate) currentDay:currentDate monthCount:2];
            break;
        case 4:
            [DateUtils getCurrentSeasonRange:(&startDate) endDay:(&endDate) currentDay:currentDate];
            break;
        default:
            break;
    }

    [self loadHistories:startDate endDay:endDate];

    [currentDate release];    
}

-(void)loadHistories:(NSDate *)startDay endDay:(NSDate *)endDay
{
    [self reloadHistories];
    
    NSArray* records = [[Database sharedInstance] getRecordsByDate:startDay endDay:endDay userId:[[ServerProxy sharedInstance] currentUserId]];
    for (Record* record in records)
    {
        [self.histories addObject:[RecordItemViewController makeWithRecord:record]];
    }
    
    [self.tableView reloadData];
}

-(void)initializeHistories
{
    NSMutableArray* histories = [[NSMutableArray alloc] init];
    self.histories = histories;
    [histories release];
}

-(void)reloadHistories
{
    [self.histories removeAllObjects];
    
    if(self.peakValueItemVC == nil)
    {
        PeakValueItemViewController* peakValueItemVC = [[PeakValueItemViewController alloc] initWithNibName:@"PeakValueItemViewController" bundle:nil];
        self.peakValueItemVC = peakValueItemVC;
        [peakValueItemVC release];
    }
    [self.histories addObject:self.peakValueItemVC];
    
    if(self.exceedStdItemVC == nil)
    {
        ExceedStdItemViewController* exceedStdItemVC = [[ExceedStdItemViewController alloc] initWithNibName:@"ExceedStdItemViewController" bundle:nil];
        self.exceedStdItemVC = exceedStdItemVC;
        [exceedStdItemVC release];  
    }
    [self.histories addObject:self.exceedStdItemVC];
}

@end
